#include "status_enum.h"
#include "student_interface.h"
#include "student_service.h"
#include "unity.h"
#include <stdlib.h>
#include <string.h>

// Test data
static student_info_t test_student1 = {
    .id = "123",
    .name = "Alice",
    .gender = FEMALE,
    .dob = "2000-01-01"};

static student_info_t test_student2 = {
    .id = "456",
    .name = "Bob",
    .gender = MALE,
    .dob = "1999-12-31"};

// Global variables for context and interface
static service_student_context_t context;
static student_interface_t interface;

// Test setup and teardown
void setUp(void)
{
        service_init_student(&context, &interface);
        context.student_interface = &interface;
}

void tearDown(void)
{
}

// Test cases
void test_service_init_student(void)
{
        status_t status = service_init_student(&context, &interface);
        TEST_ASSERT_EQUAL(STATUS_SUCCESS, status);
        TEST_ASSERT_NOT_NULL(context.student_interface);
}

void test_service_add_student(void)
{
        status_t status1 = service_add_student(&context, &test_student1);
        status_t status2 = service_add_student(&context, &test_student2);

        TEST_ASSERT_EQUAL(STATUS_SUCCESS, status1);
        TEST_ASSERT_EQUAL(STATUS_SUCCESS, status2);
}

void test_service_delete_student_id(void)
{
        student_info_t info;
        status_t status = service_delete_student_id(&context, "123", &info);
        TEST_ASSERT_EQUAL(STATUS_SUCCESS, status);
        TEST_ASSERT_EQUAL_STRING("123", info.id);
}

void test_service_delete_student_name(void)
{
        student_info_t info;
        status_t status = service_delete_student_name(&context, "Bob", &info);
        TEST_ASSERT_EQUAL(STATUS_SUCCESS, status);
        TEST_ASSERT_EQUAL_STRING("Bob", info.name);
}

/**
 * @brief 测试更新学生信息的服务函数
 *
 * 此函数用于测试 service_update_student 函数的功能。
 * 首先，向服务中添加两个测试学生的信息，然后尝试更新其中一个学生的信息。
 * 最后，验证更新操作是否成功。
 */
void test_service_update_student(void)
{
        // 向服务中添加第一个测试学生的信息
        service_add_student(&context, &test_student1);
        // 向服务中添加第二个测试学生的信息
        service_add_student(&context, &test_student2);

        // 调用 service_update_student 函数更新第一个测试学生的信息
        status_t status = service_update_student(&context, &test_student1);
        // 验证更新操作是否成功
        TEST_ASSERT_EQUAL(STATUS_SUCCESS, status);
}

void test_service_query_student_id(void)
{
        student_info_t info;
        status_t status = service_query_student_id(&context, "123", &info);
        TEST_ASSERT_EQUAL(STATUS_SUCCESS, status);
        TEST_ASSERT_EQUAL_STRING("123", info.id);
}

void test_service_query_student_name(void)
{
        student_info_t info;

        status_t status = service_query_student_name(&context, "Bob", &info);
        TEST_ASSERT_EQUAL(STATUS_SUCCESS, status);
        TEST_ASSERT_EQUAL_STRING("Bob", info.name);
}

void test_service_data_get_all_student(void)
{
        student_info_t *students = NULL;
        size_t count = 0;

        status_t status = service_get_all_student(&context,
                                                  &students, &count);

        TEST_ASSERT_EQUAL(STATUS_SUCCESS, status);
        TEST_ASSERT_NOT_NULL(students);
        TEST_ASSERT_EQUAL(2, count);

        TEST_ASSERT_EQUAL_STRING("123", students[0].id);
        TEST_ASSERT_EQUAL_STRING("Alice", students[0].name);
        TEST_ASSERT_EQUAL_STRING("456", students[1].id);
        TEST_ASSERT_EQUAL_STRING("Bob", students[1].name);

        free(students);
}

int main(void)
{
        UNITY_BEGIN();

        RUN_TEST(test_service_init_student);
        RUN_TEST(test_service_add_student);
        RUN_TEST(test_service_delete_student_id);
        RUN_TEST(test_service_delete_student_name);
        RUN_TEST(test_service_update_student);
        RUN_TEST(test_service_query_student_id);
        RUN_TEST(test_service_query_student_name);
        RUN_TEST(test_service_data_get_all_student);

        return UNITY_END();
}
